package com.yiqi.news.mvp.presenter

import android.app.Application

import com.jess.arms.integration.AppManager
import com.jess.arms.di.scope.ActivityScope
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.http.imageloader.ImageLoader
import com.yiqi.news.app.base.ActivityLifeCycle
import com.yiqi.news.app.utils.RxUtils
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.entity.User
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject

import com.yiqi.news.mvp.contract.UserinfoContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/20/2019 16:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class UserinfoPresenter
@Inject
constructor(model: UserinfoContract.Model, rootView: UserinfoContract.View) :
        BasePresenter<UserinfoContract.Model, UserinfoContract.View>(model, rootView) {
    @Inject
    lateinit var mErrorHandler: RxErrorHandler
    @Inject
    lateinit var mApplication: Application
    @Inject
    lateinit var mImageLoader: ImageLoader
    @Inject
    lateinit var mAppManager: AppManager

    fun getUser() {
        mModel.getUser()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showUser(t.data)
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }


                })
    }

    fun updateBirthday(birthday: String) {
        if (birthday.isNullOrEmpty()) {
            return
        }
        var map = HashMap<String, Any?>()
        map["birthday"] = birthday
        mModel.updateUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showUpdateSuccess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }

                })
    }


    fun updateGender(sex: Int) {

        var map = HashMap<String, Any?>()
        map["sex"] = sex
        mModel.updateUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showUpdateSuccess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }

                })
    }


    fun updateNick(username: String) {
        if (username.isNullOrEmpty()) {
            mRootView.showError("请输入昵称")
            return
        }
        var map = HashMap<String, Any?>()
        map["nickName"] = username
        mModel.updateUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showUpdateSuccess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }

                })
    }

    fun uploadAvatar(avatar: String) {

        mModel.uploadAvatar(avatar)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showUpdateSuccess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }

                })
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}
